﻿@{
    // Initialiser les variables de la page générale
    var title = "";
    var value = "";
    var reason = "";
    var date = DateTime.Now.ToShortDateString(); // default value

    Validation.RequireField("title", "You have to specify a title.");
    Validation.RequireField("value", "You have to specify a value.");

    if (IsPost)
    {
        AntiForgery.Validate();
        title = Request.Form["title"];
        value = Request.Form["value"];
        reason = Request.Form["reason"];
        date = Request.Form["date"];

        // Si toutes les informations sont valides, créer un compte
        if (Validation.IsValid())
        {
            // Insérer un nouvel utilisateur dans la base de données
            var dbcontext = new alguillet_ExpenseManagerEntities();

            var operations = dbcontext.Operations;
            Operation ope = new Operation();
            ope.Title = title;
            ope.Value = Double.Parse(value);
            ope.Reason = reason;
            ope.IDFMember = WebSecurity.CurrentUserId;
            ope.Date = DateTime.Parse(date);
            ope.DateCreation = DateTime.Now;
            //ope.DateCreation = Convert.ToDateTime(date);


            operations.Add(ope);
            var success = dbcontext.SaveChanges();

            if (success < 1)
            {
                ModelState.AddFormError("An error occurred.");
            }
        }
    }
}

<form method="post" >
@AntiForgery.GetHtml()
@Html.ValidationSummary("The operation can't be added.", excludeFieldErrors: true, htmlAttributes: null)
<fieldset>
    <legend>Add operation</legend>
    <ol>
        <li>
            <label for="title">
                Title</label>
            <input type="text" id="title" name="title" value="@title" @Validation.For("title") />
            @Html.ValidationMessage("title")
        </li>
        <li>
            <label for="value">
                Value</label>
            <input type="text" id="value" name="value" value="@value" @Validation.For("value") />
            @Html.ValidationMessage("value")
        </li>
        <li>
            <label for="reason">
                Description</label>
            <input type="text" id="reason" name="reason" value="@reason" />
        </li>
        <li>
            <label for="date">
                Transaction Date</label>
            <input type="text" id="date" name="date" value="@date" />
        </li>
    </ol>
    <input type="submit" value="Save"/>
</fieldset>
</form>

<script type="text/javascript">
    $(document).ready(function () {
        $("#value").spinner({
            step: 1.00,
            numberFormat: "n"
        });
        $("#date").datepicker(
        {
            dateFormat: "dd/mm/yy"
        }
        );
    });
</script>
